<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车次号码" prop="trainNumber">
      <el-input v-model="dataForm.trainNumber" placeholder="车次号码"></el-input>
    </el-form-item>
    <el-form-item label="车次级别, K/T/Z/G" prop="trainLevel">
      <el-input v-model="dataForm.trainLevel" placeholder="车次级别, K/T/Z/G"></el-input>
    </el-form-item>
    <el-form-item label="始发站" prop="startStation">
      <el-input v-model="dataForm.startStation" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="终点站" prop="endStation">
      <el-input v-model="dataForm.endStation" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="总运行时长，单位分钟" prop="duration">
      <el-input v-model="dataForm.duration" placeholder="总运行时长，单位分钟"></el-input>
    </el-form-item>
    <el-form-item label="当天出发时间，HH:mm" prop="departureTime">
      <el-input v-model="dataForm.departureTime" placeholder="当天出发时间，HH:mm"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          trainNumber: '',
          trainLevel: '',
          startStation: '',
          endStation: '',
          status: '',
          duration: '',
          departureTime: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          trainNumber: [
            { required: true, message: '车次号码不能为空', trigger: 'blur' }
          ],
          trainLevel: [
            { required: true, message: '车次级别, K/T/Z/G不能为空', trigger: 'blur' }
          ],
          startStation: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          endStation: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          duration: [
            { required: true, message: '总运行时长，单位分钟不能为空', trigger: 'blur' }
          ],
          departureTime: [
            { required: true, message: '当天出发时间，HH:mm不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`t31-ticket/ticket/train/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.trainNumber = data.train.trainNumber
                this.dataForm.trainLevel = data.train.trainLevel
                this.dataForm.startStation = data.train.startStation
                this.dataForm.endStation = data.train.endStation
                this.dataForm.status = data.train.status
                this.dataForm.duration = data.train.duration
                this.dataForm.departureTime = data.train.departureTime
                this.dataForm.createTime = data.train.createTime
                this.dataForm.updateTime = data.train.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`t31-ticket/ticket/train/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'trainNumber': this.dataForm.trainNumber,
                'trainLevel': this.dataForm.trainLevel,
                'startStation': this.dataForm.startStation,
                'endStation': this.dataForm.endStation,
                'status': this.dataForm.status,
                'duration': this.dataForm.duration,
                'departureTime': this.dataForm.departureTime,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
